import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:libflutter/view/LayoutLogPrint.dart';

class TestListViewWidget extends StatefulWidget {
  const TestListViewWidget({super.key});

  @override
  State<TestListViewWidget> createState() {
    return _ListViewState();
  }
}

class _ListViewState extends State<TestListViewWidget> {
  ScrollController _controller = ScrollController();
  bool showToTopBtn = false;

  @override
  void initState() {
    super.initState();
    _controller.addListener(() {
      print("滚动监听 ， ${_controller.offset}");
      if (_controller.offset < 1000 && showToTopBtn) {
        setState(() {
          showToTopBtn = false;
        });
      } else if (_controller.offset >= 1000 && showToTopBtn == false) {
        setState(() {
          showToTopBtn = true;
        });
      }
    });
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("基础组件 ListView 学习使用"),
      ),
      body: Column(
        children: [
          Text("ScrollController"),
          Expanded(
              child: ListView.builder(
            controller: _controller,
            itemExtent: 50,
            itemBuilder: (ctx, index) {
              return Text("index >> ${index}");
            },
            itemCount: 100,
          ))
        ],
      ),
      floatingActionButton: !showToTopBtn
          ? null
          : FloatingActionButton(
              onPressed: () {
                // _controller.animateTo(
                //   .0,
                //   duration: Duration(milliseconds: 200),
                //   curve: Curves.ease,
                // );

                _controller.jumpTo(
                  .0
                );
              },
              child: Text("Top"),
            ),
    );
  }
}
